package cn.exrick.xboot.modules.CheckConn;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.SpringUtils;
import cn.exrick.xboot.modules.CheckConn.entity.CheckConn;
import cn.exrick.xboot.modules.CheckConn.service.impl.CheckConnImpl;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: njp
 * @Date: 2020/05/27/15:47
 * @Description:
 */
@Slf4j
//@RestController
//@RequestMapping("/xboot")
@Service
public class CheckConnThread implements Runnable{
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private CheckConnImpl checkConnimpl;
    private   CheckConn checkConn;

    public void setCheckConn(CheckConn checkConn){
        this.checkConn=checkConn;
    }
    //通过构造函数向线程中传值
    public CheckConnThread(CheckConn checkConn) {
        this.checkConn = checkConn;
    }
    public CheckConnThread() {

    }


    @Override
    public void run() {

        try {
            checkConnimpl= (CheckConnImpl)SpringUtils.getBean("checkConnImpl");
            log.info("任务执行开始:" + new Date());
            //检测连接状态（在redis中一个生命周期是period 的数据）
            checkConnimpl.getRdisvalueByKey(checkConn);
            log.info("任务执行完毕:" + new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public  void timeOut( CheckConn checkConn){
        this.checkConn=checkConn;
        /**使用Executors工具快速构建对象*/
        ScheduledExecutorService scheduledExecutorService =
                Executors.newScheduledThreadPool(1);
        log.info(checkConn.getPeriod()+"秒后开始执行计划线程池服务..." + new Date());
        //njp +1给系统预留1秒思考时间
        scheduledExecutorService.schedule(new CheckConnThread(checkConn), checkConn.getPeriod()+1, TimeUnit.SECONDS);
    }

    /*@RequestMapping(value = "/mytest", method = RequestMethod.GET)
    @ApiOperation(value = "test")
    public void test(){
        checkConn=new CheckConn(6,"","FD");
        checkConn=checkConn;
        String value=null;
        value = redisTemplate.opsForValue().get(CommonConstant.checkConn + checkConn.getSender());
        if(StrUtil.isBlank(value)){
            //如果redis中没有对应的值说明连接异常
            checkConnimpl.updateCheckConnStatus();
        }

        //return new ResultUtil<List<CommonLink>>().setData(list);
    }*/

}
